package com.platform.common.utils.wx;

import com.platform.modules.common.utils.NonceUtil;
import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;
import java.util.Map;

public class SignUtils {
    private String APPID = "wxae0f0d0aef1a8645";
    private String API_KEY = "26fd8e0d4fe102b8e37fc421f1277684";
    private static SignUtils instance;
    public static SignUtils getInstance() {
        if (instance == null) {
            instance = new SignUtils();
        }
        return instance;
    }

    /**
     * 具体生成签名signature
     */
    public Map<String,String> wxSignature(String url) {
        String accessToken = GetAccessTokenUtil.getAccessToken(APPID,API_KEY);
        String ticket = JsapiTicketUtil.JsapiTicket(accessToken);
        String noncestr = NonceUtil.createNonce(16);//随机字符串
        String timestamp = String.valueOf(System.currentTimeMillis() / 1000);//时间戳
        String str = "jsapi_ticket="+ticket+"&noncestr="+noncestr+"&timestamp="+timestamp+"&url="+url;
        String digest = new SHA1().getDigestOfString(str.getBytes());
        String signature = digest.toLowerCase();// signature
        Map<String,String> map = new HashMap();
        map.put("timestamp",timestamp);
        map.put("accessToken",accessToken);
        map.put("ticket",ticket);
        map.put("noncestr",noncestr);
        map.put("signature",signature);
        map.put("appid",APPID);
        return map;
    }
}
